package org.jeecg.modules.meter.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.ForeignResult;
import org.jeecg.entity.meter.Meter;
import org.jeecg.entity.meter.MeterControlSwitch;
import org.jeecg.enums.meter.MeterCallBackTypeEnum;
import org.jeecg.modules.meter.service.IMeterControlSwitchService;
import org.jeecg.modules.meter.service.IMeterService;
import org.jeecg.vo.meter.ForeignMeterCallBackReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 水电表硬件回调处理
 * @Author: liurui
 * @Date:   2020-12-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class ForeignMeterCallBackServiceImpl {

    @Autowired
    private IMeterControlSwitchService meterControlSwitchService;

    @Autowired
    private IMeterService meterService;

    public ForeignResult serviceCallback(ForeignMeterCallBackReq callBackReq) {
        if (MeterCallBackTypeEnum.METER_CALL_BACK_TYPE_2.getCode().equals(callBackReq.getType())){  //通断状态控制
            MeterControlSwitch meterControl = meterControlSwitchService.selectById(callBackReq.getRequestCode());
            if (meterControl == null){
                return ForeignResult.error("未查到请求码对应的记录！");
            }
            if (callBackReq.getCode() == 0){  //处理成功
                meterControlSwitchService.updateCallbackStatus(meterControl);
                Meter meter = new Meter();
                meter.setId(meterControl.getMeterId());
                if ("1".equals(meterControl.getCommand())){  //1.开  2.关
                    meter.setSwitchStatus(1);
                    meterService.updateSwitchStatus(meter);
                } else if ("2".equals(meterControl.getCommand())){//
                    meter.setSwitchStatus(2);
                    meterService.updateSwitchStatus(meter);
                }
            } else {
                log.warn("仪表通断回调执行结果失败！信息:{}",callBackReq);
            }
        }
        return ForeignResult.ok();
    }
}
